package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.ManageParam
import com.cheyun.netsalev3.bean.MoneyParam
import com.cheyun.netsalev3.bean.NoticeData
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class HomeTaskFragment1Repository : BaseRepository() {
    fun getManagelist(stime: String, kFunction1: KFunction1<@ParameterName(name = "p") ManageParam, Unit> ,loading:Boolean=true) {
        if (loading){
            networkState.postValue( NetworkState.LOADING)
        }

        ApiClient.getInstance().getManagelist(stime)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ManageParam>>() {
                    override fun onSuccess(t: BaseResp<ManageParam>) {
                        if (loading){
                            networkState.postValue( NetworkState.LOADED)
                        }
                        t.data?.apply {
                            for (i in 0 until this.data.size) {
                                this.data[i].initData()
                            }

                        }
                        t.data?.apply(kFunction1)
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue( NetworkState.LOADED)
                    }


                })

    }
    var uid = MySharedPreferences.getLoginData()?.member?.uid ?: 0
    /**
     * 获取消息
     */
    fun GetMessage(kFunction1: KFunction1<@ParameterName(name = "item") NoticeData, Unit>) {
        ApiClient.getInstance().noticeDatalist(uid, 1,1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<NoticeData>>() {
                    override fun onSuccess(t: BaseResp<NoticeData>) {
                        t.data?.apply {
                            kFunction1(this)
                        }
                    }
                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun getMoney( moth: (MoneyParam) -> Unit){
//        ApiClient.getInstance().getMoney()
//                .subscribeOn(Schedulers.io())
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .subscribe(object : ApiCallback<BaseResp<MoneyParam>>() {
//                    override fun onSuccess(t: BaseResp<MoneyParam>) {
//                        moth(t?.data!!)
//                    }
//                    override fun onError(ex: ApiException) {
//
//                    }
//                })
    }
}